package org.example;

import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) throws MalformedURLException {

        //http://vip.123pan.cn/13/files/1.txt?auth_key=1689220731-123-13-md5函数(/13/files/1.txt-1689220731-123-13-289ds32418bxdba)
        //https://vip.123pan.cn/1813533250/outputs/client_game_res/3.4_live/output_12591909_58182dc06f/client/Android/release_res_versions_external
        String ResourceUrl = "https://vip.123pan.cn/1813533250/outputs/client_game_res/3.4_live";
        System.out.println(getAuth(ResourceUrl));
    }

    public static String getAuth(String url) {
        // 获取10位时间戳
        long timestamp = System.currentTimeMillis() / 1000;
        // 随机生成10位数
        int random = (int) (Math.random() * 1000000000);
        String uid = "1813533250";
        Pattern pattern = Pattern.compile("https://vip\\.123pan\\.cn(/.+)$");
        Matcher matcher = pattern.matcher(url);
        String subString = null;
        if (matcher.find()) {
            subString = matcher.group(1);
            System.out.println(subString);
        }
        String md5hash = getMD5Hash(subString + "-" + timestamp + "-" + random + "-" + uid + "-" + "123456");
        /*System.out.println("auth: " + getMD5Hash(timestamp + "-" + random + "-" + uid + "-" + md5hash));
        System.out.println("url: " + url + "?auth_key=" + timestamp + "-" + random + "-" + uid + "-" + md5hash);*/
        return url + "?auth_key=" + timestamp + "-" + random + "-" + uid + "-" + md5hash;
    }

    public static String getMD5Hash(String text) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(text.getBytes(StandardCharsets.UTF_8));
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
}